package com.gvillain.perso.mail;

import java.io.IOException;
import java.util.Properties;

import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.Message.RecipientType;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

/**
 * Classe d'envois des messages
 * @author guillaume villain 
 * Project : Javamail 
 * Class : org.insia.vmail.mail.smtp 
 * File : SmtpMail.java 
 * Created : 13 nov. 06 19:45:06
 */
/**
 * @author Administrateur
 * Project : Lambda
 * Class : org.insia.vmail.mail.smtp
 * File : SmtpMail.java
 * Created : Jan 14, 2007 10:23:58 AM
 */
/**
 * @author Administrateur
 * Project : Lambda
 * Class : org.insia.vmail.mail.smtp
 * File : SmtpMail.java
 * Created : Jan 14, 2007 10:24:05 AM
 */
public class SmtpMail {
    private final static String HOST = "mail.smtp.host";
    private final static String FROM = "";
    private final static String TO = "";
    private Properties prop = new Properties();
    private Session session = null;
    private MimeMessage msg = null;
    private MailAuthentificator auth = null;

    
    /**
     * Constructeur
     * @param adressFrom
     * @param adressTo
     * @param mailHost
     * @param auth
     */
    public SmtpMail(String mailHost,
                    MailAuthentificator auth) {
        prop.put(HOST, mailHost);
        this.auth = auth;
    }

    
    /**
     * Initialise la classe et l'envoi du message
     * @param Subject
     * @throws AddressException
     * @throws MessagingException
     */
    public void Initialize() throws AddressException,
            MessagingException {
        if (auth != null) {
            prop.put("mail.smtp.auth", "true");
            prop.put("mail.smtps.auth", "true");
            session = Session.getInstance(prop, auth);
        } else {
            session = Session.getDefaultInstance(prop, auth);
        }
        
      
    }

    
    /**
     * Configure le port smtp
     * @param port
     */
    public void setPortSmtp(int port) {
        prop.put("mail.smtp.port", String.valueOf(port));
        prop.put("mail.smtps.port", String.valueOf(port));
    }

    public void setHostSmtp(String host) {
        prop.put("mail.smtp.host", host);
        prop.put("mail.smtps.host", host);
    }

    /**
     * Met le message dans le mail en texte ou html
     * @param Body
     * @param Html
     * @throws MessagingException
     */
    public void setText(String Body, boolean Html) throws MessagingException {
        if (Html == true) {
            msg.setContent(Body, "text/html");
        } else {
            msg.setText(Body);
        }
    }

  
    /**
     * Envois le mail
     * @throws MessagingException
     */
    public void SendMail(Message msg) throws MessagingException {
        this.msg = new MimeMessage(session);
        try {
			this.msg.setText(msg.getContent().toString());
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
        this.msg.setFrom(new InternetAddress(FROM));
        this.msg.setRecipient(RecipientType.TO, new InternetAddress(TO));
        this.msg.setSubject(msg.getSubject());
        this.msg.setSentDate(msg.getSentDate());
        Transport.send(this.msg);
    }

    /**
     * Envoie le mail sur un serveur avec du ssl
     * @throws MessagingException
     */
    public void SendMailSSL() throws MessagingException {
        Transport trans = session.getTransport("smtps");
        trans.connect(prop.getProperty(HOST), auth.getPasswordAuthentication()
                .getUserName(), auth.getPasswordAuthentication().getPassword());
        trans.sendMessage(msg, msg.getAllRecipients());
        trans.close();
    }


}
